package com.summer.service.impl;

import com.summer.entity.PlatformRoleAuthority;
import com.summer.mapper.PlatformAuthorityMapper;
import com.summer.mapper.PlatformRoleAuthorityMapper;
import com.summer.service.IPlatformRoleAuthorityService;
import com.summer.vo.PlatformRoleAuthorityVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色权限Service
 */
@Slf4j
@Service
public class PlatformRoleAuthorityService implements IPlatformRoleAuthorityService {

    @Resource
    private PlatformRoleAuthorityMapper roleAuthorityMapper;

    @Resource
    private PlatformAuthorityMapper platformAuthorityMapper;

    /**
     * 根据角色id查询权限
     * @param roleId
     * @return
     */
    @Override
    public PlatformRoleAuthority findByRoleId(Integer roleId) {
        log.info("【查看权限】根据角色id查询权限，roleId={}",roleId);
        return  roleAuthorityMapper.findByRoleId(roleId);
    }

    /**
     * 根据角色id删除角色权限记录
     * @param roleId
     * @return
     */
    @Override
    public void deleteByRoleId(Integer roleId) {
        log.info("【删除角色的权限】根据角色id删除权限，roleId={}",roleId);
         roleAuthorityMapper.deleteByRoleId(roleId);
    }

    /**
     * 设置角色权限
     * @param roleId
     * @param ids
     */
    @Override
    public void setAuthority(Integer roleId, String ids) {
        log.info("【设置角色权限】设置角色权限，roleId={},ids={}",roleId,ids);
        // 删除该角色所有的权限
        roleAuthorityMapper.deleteByRoleId(roleId);

        // 添加新的权限
        String[] funcList = ids.split(",");
        for(String id:funcList){
            PlatformRoleAuthority relation = new PlatformRoleAuthority();
            relation.setRoleId(roleId);
            relation.setAuthorityId(Integer.parseInt(id));
            this.roleAuthorityMapper.insert(relation);
        }
    }

    /**
     * 角色权限列表
     * @param id
     * @return
     */
    @Override
    public List<PlatformRoleAuthorityVo> findRoleAuthority(Integer id) {
        log.info("【角色权限列表】角色权限列表,id={}",id);
        List<PlatformRoleAuthorityVo> authority = platformAuthorityMapper.findRoleAuthority(id);
        return authority;
    }
}
